package com.demo.service.impl;

import com.demo.dao.AccountDao;
import com.demo.pojo.Account;
import com.demo.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AccountServiceImpl implements AccountService {
    @Autowired
    AccountDao accountDao;
    @Override
    public void transfer(Account transferOut, Account transferIn, Double price) {
        if (price < 0) {
            throw new IllegalArgumentException("转出的金额不能小于0!");
        }
        if (price > transferOut.getPrice()) {
            throw new RuntimeException("账户余额不足!");
        }
        transferOut.setPrice(transferOut.getPrice() - price);
        transferIn.setPrice(transferIn.getPrice() + price);
        accountDao.update(transferOut);
        int i = 1 / 0;
        accountDao.update(transferIn);
    }

    @Test
    public void test01() {
        Account account1 = new Account(1L, null, 20000.0);
        Account account2 = new Account(2L, null, 10000.0);
        transfer(account1, account2, 100.0);
    }
}
